package mall.core.jdbc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
public class QueryWrapper {

    private final StringBuilder sql = new StringBuilder();
    private final List<Object> params = new ArrayList<>();

    public QueryWrapper(String table) {
        this.sql.append("select * from ").append(table).append(" where 1=1");
    }

    public QueryWrapper eq(Boolean b, String column, Object value) {
        if (b) {
            sql.append(" and ").append(column).append(" = ?");
            params.add(value);
        }
        return this;
    }

    public QueryWrapper like(Boolean b, String column, Object value) {
        if (b) {
            sql.append(" and ").append(column).append(" like ?");
            params.add("%" + value + "%");
        }
        return this;
    }

    public QueryWrapper limit(Integer offset, Integer size) {
        sql.append(" limit ?, ?");
        params.add(offset);
        params.add(size);
        return this;
    }

    public String getSql() {
        return sql.toString();
    }

    public Object[] getParams() {
        return params.toArray();
    }


}
